package com.zzh.controller;

import com.zzh.domain.Menu;
import com.zzh.domain.ResponseResult;
import com.zzh.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        List<Menu> menus = menuService.findAllMenu();
        return new ResponseResult(true, 200, "成功响应", menus);
    }

    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(@RequestParam int id) {
        if (id == -1) {
            //添加操作，不需要回显信息
            List<Menu> menus = menuService.findSubMenuListByPid(-1);
            Map<String, Object> map = new HashMap<>();
            map.put("menuInfo", null);
            map.put("parentMenuList", menus);
            return new ResponseResult(true, 200, "成功响应", map);
        }else {
            //修改操作，需要回显信息
            Menu menu = menuService.findMenuById(id);
            List<Menu> menus = menuService.findSubMenuListByPid(-1);
            Map<String, Object> map = new HashMap<>();
            map.put("menuInfo", menu);
            map.put("parentMenuList", menus);
            return new ResponseResult(true, 200, "成功响应", map);
        }
    }

    /**
     *  保存和修改菜单
     */
//    @RequestMapping("/saveOrUpdateMenu")
//    public ResponseResult saveOrUpdateMenu(Menu menu) {
//
//    }


}
